<template>
  <div class="formDiv">
    <el-card class="box-card">
      <div class="back">
        <div>
          <el-button
            class="submission priamry_border"
            style="display: flex; float: right"
            @click="back"
            >返回</el-button
          >
        </div>
      </div>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        clearable
        label-width="102px"
        :rules="rules"
        class="formArea"
      >
        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 区域 -->
              <el-form-item label="区域:" prop="region">
                <el-cascader
                  size="large"
                  :options="pcaTextArr"
                  v-model="ruleForm.region"
                  :props="{ checkStrictly: true }"
                  clearable
                >
                </el-cascader>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"
            ><div>
              <!-- 店铺详细地址 -->
              <el-form-item label="店铺地址 :" prop="address">
                <el-input
                  v-model="ruleForm.address"
                  placeholder="请输入店铺详细地址"
                ></el-input>
              </el-form-item></div
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 联系人 -->
              <el-form-item label="联系人：" prop="contact">
                <el-input
                  v-model="ruleForm.contact"
                  placeholder="请输入联系人"
                ></el-input>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"
            ><div>
              <!-- 联系电话 -->
              <el-form-item label="联系电话：" prop="phone">
                <el-input
                  v-model="ruleForm.phone"
                  placeholder="请输入联系电话"
                ></el-input>
              </el-form-item></div
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 店铺介绍 -->
              <el-form-item label="店铺介绍：" prop="introduce">
                <el-input
                  v-model="ruleForm.introduce"
                  placeholder="请输入店铺介绍"
                ></el-input>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"
            ><div>
              <!-- 备注 -->
              <el-form-item label="备注：" prop="remark">
                <el-input
                  v-model="ruleForm.remark"
                  placeholder="请输入备注"
                ></el-input>
              </el-form-item></div
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 店铺名称 -->
              <el-form-item label="店铺名称:" prop="shopName">
                <el-input
                  v-model="ruleForm.shopName"
                  placeholder="请输入店铺名称"
                ></el-input>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"> </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="picUpload">
              <el-form-item label="店铺logo：" prop="shopLog">
                <div
                  class="upLoadPicBox"
                  @click="modalPicTap('1')"
                  :disabled="isDisabled"
                >
                  <div v-if="ruleForm.shopLog" class="pictrue">
                    <img :src="ruleForm.shopLog" />
                  </div>
                  <div v-else class="upLoad">
                    <i class="el-icon-camera cameraIconfont" />
                  </div>
                </div>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="picUpload">
              <el-form-item label="广告图片：">
                <div class="upLoadPicBox">
                  <div
                    v-for="(item, index) in ruleForm.advertises"
                    :key="index"
                    class="pictrue"
                    draggable="true"
                  >
                    <img :src="item" />
                    <i
                      v-if="!isDisabled"
                      class="el-icon-error btndel"
                      @click="handleRemove(index)"
                    />
                  </div>
                  <div
                    v-if="ruleForm.advertise != [] && !isDisabled"
                    @click="modalPicTap('2')"
                  >
                    <div class="upLoad">
                      <i class="el-icon-camera cameraIconfont" />
                    </div>
                  </div>
                </div>
              </el-form-item>
            </div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12"
            ><div>
              <el-form-item class="buttons">
                <el-button type="primary" @click="submitForm('ruleForm')"
                  >保存</el-button
                >
                <el-button @click="cancel()">取消</el-button>
              </el-form-item>
            </div></el-col
          >
          <el-col :span="12"><div></div></el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { seeShopApi, editShopApi } from "@/api/aishop";
import { pcaTextArr } from "element-china-area-data";
export default {
  name: "shopDetail",
  created() {
    this.getShop(); //根据 id 查询店铺信息
  },
  data() {
    return {
      isDisabled: this.$route.params.isDisabled === "1" ? true : false,
      pcaTextArr,
      //表单
      ruleForm: {
        address: "", //店铺详细地址
        advertises: [], //广告图片
        collect: 0,
        contact: "", //联系人
        phone: "", //联系电话
        id: 0,
        introduce: "", //店铺介绍
        region: [], //区域
        remark: "", //备注
        shopLog: "", //店铺logo
        shopName: "", //店铺名称
        province: "", //省
        city: "", //市
        county: "", //县
      },
      area: [],
      rules: {
        //店铺详细地址
        address: [
          { required: true, message: "请输入店铺详细地址", trigger: "blur" },
        ],
        //联系人
        contact: [{ required: true, message: "请输入联系人", trigger: "blur" }],
        //联系电话
        phone: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
        //店铺介绍
        introduce: [
          { required: true, message: "请输入店铺介绍", trigger: "blur" },
        ],
        // 区域
        region: [{ required: true, message: "请输入区域", trigger: "blur" }],
        //备注
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
        //店铺名称
        shopName: [
          { required: true, message: "请输入店铺名称", trigger: "blur" },
        ],
        //广告图片
        advertise: [
          { required: true, message: "请选择广告图片", trigger: "blur" },
        ],
        //店铺logo
        shopLog: [
          { required: true, message: "请选择店铺logo", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //根据 id 查询店铺信息
    async getShop() {
      const id = JSON.parse(sessionStorage.getItem("datatwo"));
      const res = await seeShopApi({ id });
      this.area.push(res.province);
      if (res.city) {
        this.area.push(res.city);
      }
       if (res.county) {
        this.area.push(res.county);
      }
      this.ruleForm.region = this.area;
      this.ruleForm.address = res.address;
      this.ruleForm.contact = res.contact;
      this.ruleForm.remark = res.remark;
      this.ruleForm.shopName = res.shopName;
      this.ruleForm.shopLog = res.shopLog;
      this.ruleForm.introduce = res.introduce;
      this.ruleForm.phone = res.phone;
      this.ruleForm.advertises = res.advertises;
      this.ruleForm.score = res.score;
      this.ruleForm.id = res.id;
    },
    //删除选择的店铺图片
    handleRemove(i) {
      this.ruleForm.advertises.splice(i, 1);
    },
    // 上传店铺图片
    modalPicTap(tit, num) {
      const _this = this;
      if (_this.isDisabled) return;
      _this.$modalUpload(
        function (img) {
          if (tit === "1" && !num) {
            _this.ruleForm.shopLog = img[0].sattDir;
          }
          if (tit === "2" && !num) {
            if (img.length >= 10)
              return this.$message.warning("最多选择10张图片！");
            if (img.length + _this.ruleForm.advertises.length > 10)
              return this.$message.warning("最多选择10张图片！");
            img.map((item) => {
              _this.ruleForm.advertises.push(item.sattDir);
            });
          }
        },
        tit,
        "content"
      );
    },
    //返回上一页
    back() {
      this.$router.go(-1);
    },

    //点击保存后的提交
    submitForm() {
      //兜底校验
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return;
        this.ruleForm.province = this.ruleForm.region[0];
        this.ruleForm.city = this.ruleForm.region[1];
        this.ruleForm.county = this.ruleForm.region[2];
        //通过后发请求
        await editShopApi(this.ruleForm);
        this.$message.success("修改成功");
        this.$refs.ruleForm.resetFields();
        this.$router.push("/aimanage/aishop/index");
      });
    },
    //取消按钮
    cancel() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="scss">
.formDiv {
  margin-top: 40px;
  margin-left: 30px;
  .back {
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: end;
  }
  .formArea {
    padding: 0 30px;
    .province {
      width: 10vw;
      .el-input__inner {
        width: 10vw;
      }
    }
    .picUpload {
      width: 800px;
      height: 100px;
      .upLoadPicBox {
        display: flex;
        width: 800px;
        height: 70px;
      }
    }
    .pictrue {
      width: 60px;
      height: 60px;
      border: 1px dotted rgba(0, 0, 0, 0.1);
      margin-right: 10px;
      position: relative;
      cursor: pointer;
    }
    .btndel {
      position: absolute;
      z-index: 1;
      width: 20px !important;
      height: 20px !important;
      left: 46px;
      top: -4px;
    }
  }
  .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-col {
    border-radius: 4px;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }

  .el-input__inner {
    width: 30vw;
  }
  .el-select .el-select--medium {
    width: 40vw;
  }
  .buttons {
    width: 100%;
    height: 50px;
  }
}
.upload {
  width: 80px;
  height: 80px;
  // display: flex;
  text-align: center;
  line-height: 80px;
  background-color: pink;
  border: 1px solid #fff;
}
</style>